package com.ichina.chat.action;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;

import com.ichina.chat.dao.ServerDao;
import com.ichina.chat.daoall.ObjectDao;
import com.ichina.chat.vo.Question;
import com.ichina.chat.vo.Server;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class ChatAction extends ActionSupport implements  ServletRequestAware, ServletResponseAware,ServletContextAware {

	private HttpServletRequest request;
	private HttpServletResponse response;
	private ServletContext servletContext;
	private String question;
	private String replyMessage;
	private Server server;
	private String sname;
	@Resource private ServerDao serverDao;
	@Resource private ObjectDao objectDao;
	private String user;
	private Question q;
	private String result = "error";
	private String listMessage;
	private Map<String, List<String>> map;
	private String userlist;
	private List<String> userList;
	
	public String reg(){
		String name = (String)servletContext.getAttribute("Yname");
		userList = (List<String>)servletContext.getAttribute("userList");
		if(user != null){
			try{
				user = java.net.URLDecoder.decode(user,"UTF-8");
			}catch(Exception e){e.printStackTrace();}
		}
		if(userList != null){
			if(user != null){
				if(name != null){
					//用户替换
					if(!name.equals(user)){
						boolean flag = false;
						for(String s : userList){//判断传过来的是否有登录
							if(user.equals(s)){
								flag = true;
							}
						}
						if(flag){
							name = user;
						}
						servletContext.setAttribute("Yname", name);
					}
				}else{
					servletContext.setAttribute("Yname", user);
				}
			}
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public String userlist(){
		userList = (List<String>)request.getSession().getAttribute("userList");
		try {
			userlist = new String(userlist.getBytes("iso-8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		if(userList == null){
			userList = new ArrayList<String>();
		}
		if(userlist != null && !"".equals(userlist)){
			for(int i = 0;i < userList.size();i++){
				if(userlist.equals(userList.get(i))){
					userList.remove(i);
				}
			}
			userList.add(userlist);
		}
		request.getSession().setAttribute("userList", userList);
		return "ct";
	}
	@SuppressWarnings("unchecked")
	public String delUserMessage(){
		servletContext.removeAttribute("Yname");
		try{
			if(user != null){
				user = java.net.URLDecoder.decode(user,"UTF-8");
				Map<String,String> map = (Map<String,String>)servletContext.getAttribute("map");
				if(map != null){
					map.remove(user);
				}
				userList = (List<String>)servletContext.getAttribute("userList");
				if(userList != null){
					for(int i =0 ;i<userList.size(); i++){
						if(user.equals(userList.get(i))){
							userList.remove(i);
						}
					}
					servletContext.setAttribute("userList", userList);
				}
			}
		}catch(Exception e){e.printStackTrace();}
		return null;
	}

	public String reqUser() {
		if(user != null){
			try{
				user = java.net.URLDecoder.decode(user,"UTF-8");
			}catch(Exception e){e.printStackTrace();}
			ServletActionContext.getRequest().setAttribute("user", user);
		}
		result = "message";
		return result;
	}

	@SuppressWarnings("unchecked")
	public String loadServer(){
		try {
			List<Server> list = (List<Server>)objectDao.getHql("from Server where state=1");
			if(list != null){
				Server s = list.get(0);
				response.getOutputStream().print(s.getSname());
			}else{			
				response.getOutputStream().print("但前沒有客服在綫");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public String login() {
		try {
			if(serverDao.login(server.getSname())){
				result = "login";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@SuppressWarnings("unchecked")
	public String loginUser() {
		userList = (List<String>)servletContext.getAttribute("userList");
		if(userList == null){
			userList = new ArrayList<String>();
		}
		try {
			String ip = request.getRemoteAddr();
			request.setAttribute("userss",ip);
			boolean flag = true;
			for(String s : userList){
				if(("游客"+ip).equals(s)){
					flag = false;
				}
			}
			if(flag){
				userList.add("游客"+ip);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		servletContext.setAttribute("userList", userList);
		return "loginUser";
	}
	
	public String logout(){
		try {
			if(serverDao.logout(server.getSname())){
				result = "logout";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public String getQuestion() {
		return question;
	}

	public void setQuestion(String question) {
		this.question = question;
	}

	public String getReplyMessage() {
		return replyMessage;
	}

	public void setReplyMessage(String replyMessage) {
		this.replyMessage = replyMessage;
	}

	public Server getServer() {
		return server;
	}

	public void setServer(Server server) {
		this.server = server;
	}

	public Question getQ() {
		return q;
	}

	public void setQ(Question q) {
		this.q = q;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getListMessage() {
		return listMessage;
	}

	public void setListMessage(String listMessage) {
		this.listMessage = listMessage;
	}

	public String getSname() {
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

	public Map<String, List<String>> getMap() {
		return map;
	}

	public void setMap(Map<String, List<String>> map) {
		this.map = map;
	}

	public String getUserlist() {
		return userlist;
	}

	public void setUserlist(String userlist) {
		this.userlist = userlist;
	}

	public List<String> getUserList() {
		return userList;
	}

	public void setUserList(List<String> userList) {
		this.userList = userList;
	}

	public void setServletRequest(HttpServletRequest request) {   
        this.request = request;   
    }   
  
    public void setServletResponse(HttpServletResponse response) {   
        this.response = response;   
    }

	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

}
